package com.zp.resource.psychological;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;
import com.sun.jersey.spi.resource.Singleton;
import com.zp.bean.common.ResultCommonVo;
import com.zp.bean.psychological.BaseOrganizationVo;
import com.zp.bean.psychological.OrganizationVo;
import com.zp.bean.psychological.ResultBaseOrganizationVo;
import com.zp.bean.psychological.ResultOrganizationVo;
import com.zp.bean.user.BaseUserVo;
import com.zp.bean.user.ResultBaseUserVo;
import com.zp.domain.ModuleOrganization;
import com.zp.domain.Organization;
import com.zp.domain.PersonOrganization;
import com.zp.domain.Type;
import com.zp.resource.common.BaseServices;
import com.zp.util.Constant;
import com.zp.util.FileUtil;
import com.zp.util.MyException;

/**
 * organization services
 * 
 * @author chenlijun08@gmail.com
 * 
 * @version 1.0
 * 
 * @date 2014/3/4
 * 
 */
@Component
@Path("/rest/organization")
@Singleton
public class OrganizationServices extends BaseServices {

	@POST
	@Path("/addO")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response addType(Organization o) {
		ResultCommonVo result = new ResultCommonVo();
		try {
			jpaDao.save(o);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * add organization information path:/organization/addOrganization HTTP:POST
	 * 
	 * @param name
	 *            name of organization
	 * @param summary
	 * @param introduce
	 * @param address
	 * @param character
	 * @param manage
	 * @param cityCode
	 *            which city the organization in
	 * @param phone
	 * @param weibo
	 * @param members
	 * @param image
	 *            title image of organization
	 * @return Response, ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":0}
	 */
	@POST
	@Path("/addOrganization")
	@Consumes(MediaType.MULTIPART_FORM_DATA)
	@Produces(MediaType.APPLICATION_JSON)
	public Response addOrganization(@FormDataParam("name") String name,
			@FormDataParam("summary") String summary,
			@FormDataParam("introduce") String introduce,
			@FormDataParam("address") String address,
			@FormDataParam("character") String character,
			@FormDataParam("manage") String manage,
			@FormDataParam("cityCode") String cityCode,
			@FormDataParam("phone") String phone,
			@FormDataParam("weibo") String weibo,
			@FormDataParam("members") int members,
			@FormDataParam("image") InputStream image,
			@FormDataParam("image") FormDataContentDisposition fileDisposition,
			@Context ServletContext context) {
		ResultCommonVo result = new ResultCommonVo();
		try {
			Organization organization = new Organization();
			organization.setAddress(address);
			organization.setCharacter(character);
			organization.setCityCode(cityCode);
			organization.setImage(FileUtil.getInstance().saveFile(image,
					fileDisposition, context));
			organization.setIntroduce(introduce);
			organization.setManage(manage);
			organization.setMembers(members);
			organization.setName(name);
			organization.setPhone(phone);
			organization.setSummary(summary);
			organization.setWeibo(weibo);
			jpaDao.save(organization);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * update organization information path:/organization/updateOrganization
	 * HTTP:PUT
	 * 
	 * @param id
	 *            primary key of organization
	 * @param name
	 *            name of organization
	 * @param summary
	 * @param introduce
	 * @param address
	 * @param character
	 * @param manage
	 * @param cityCode
	 *            which city the organization in
	 * @param phone
	 * @param weibo
	 * @param members
	 * @param image
	 *            title image of organization
	 * @return Response, ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":0}
	 */
	@PUT
	@Path("/updateOrganization")
	@Consumes(MediaType.MULTIPART_FORM_DATA)
	@Produces(MediaType.APPLICATION_JSON)
	public Response updateOrganization(@FormDataParam("id") String id,
			@FormDataParam("name") String name,
			@FormDataParam("summary") String summary,
			@FormDataParam("introduce") String introduce,
			@FormDataParam("address") String address,
			@FormDataParam("character") String character,
			@FormDataParam("manage") String manage,
			@FormDataParam("cityCode") String cityCode,
			@FormDataParam("phone") String phone,
			@FormDataParam("weibo") String weibo,
			@FormDataParam("members") int members,
			@FormDataParam("image") InputStream image,
			@FormDataParam("image") FormDataContentDisposition fileDisposition,
			@Context ServletContext context) {
		ResultCommonVo result = new ResultCommonVo();
		try {
			Organization organization = (Organization) this.jpaDao.findById(
					Organization.class, id);
			organization.setAddress(address);
			organization.setCharacter(character);
			organization.setCityCode(cityCode);
			organization.setImage(FileUtil.getInstance().saveFile(image,
					fileDisposition, context));
			organization.setIntroduce(introduce);
			organization.setManage(manage);
			organization.setMembers(members);
			organization.setName(name);
			organization.setPhone(phone);
			organization.setSummary(summary);
			organization.setWeibo(weibo);
			jpaDao.update(organization);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * delete Organization by id path:/organization/deleteOrganization
	 * Http:DELETE
	 * 
	 * @param organizationId
	 *            primary key of Organization
	 * 
	 * @return Response, ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":0}
	 */
	@DELETE
	@Path("/deleteOrganization")
	@Produces(MediaType.APPLICATION_JSON)
	public Response deleteOrganization(
			@QueryParam("organizationId") String organizationId) {
		logger.info("deleteOrganization called organizationId:"
				+ organizationId);
		ResultCommonVo result = new ResultCommonVo();
		try {
			jpaDao.deleteById(Organization.class, organizationId);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * get Organization by id path:/organization/getOrganization Http:POST
	 * 
	 * @param organizationId
	 *            primary key of Organization
	 * 
	 * @return Response, ResultOrganizationVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":0,"vo":{"id":
	 *         "402880e44492d4ac014492d533850001"
	 *         ,"name":"四川大学青年志愿者协会","image":null
	 *         ,"summary":"四川大学青少年行动起始于","cityType"
	 *         :null,"introduce":"四川大学青少年行动起始于1994年3月...."
	 *         ,"address":"四川省成都市武侯区科华路"
	 *         ,"character":"非营利性组织","manage":"四川大学团委","phone"
	 *         :"028-88888888","weibo"
	 *         :"http://weibo.com/schxyx","members":150},"list":null}
	 * 
	 */
	@POST
	@Path("/getOrganization")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getOrganizationById(
			@QueryParam("organizationId") String organizationId) {
		logger.info("getOrganizationById called organizationId:"
				+ organizationId);
		ResultOrganizationVo result = new ResultOrganizationVo();
		Organization organization = null;
		try {
			organization = (Organization) jpaDao.findById(Organization.class,
					organizationId);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		if (organization != null) {
			OrganizationVo vo = new OrganizationVo();
			vo.setAddress(organization.getAddress());
			vo.setCharacter(organization.getCharacter());
			vo.setCityType(this.getTypeByCode(organization.getCityCode()));
			vo.setId(organization.getId());
			if (organization.getImage() != null) {
				vo.setImage(this.serverPath + organization.getImage());
			}
			vo.setIntroduce(organization.getIntroduce());
			vo.setManage(organization.getManage());
			vo.setMembers(organization.getMembers());
			vo.setName(organization.getName());
			vo.setPhone(organization.getPhone());
			vo.setSummary(organization.getSummary());
			vo.setWeibo(organization.getWeibo());
			result.setVo(vo);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} else {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(Constant.OBJECT_NOT_EXITS);
		}
		return Response.ok(result).build();
	}

	/**
	 * get Organization with name or city code
	 * path:/organization/getOrganizations Http:POST
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @param moduleCode
	 *            the code of module
	 * @param value
	 *            name of organization or city code of organization if ignore
	 *            the default value will be null
	 * @return Response ResultOrganizationVo with JSON String
	 *         like:{"code":268,"message"
	 *         :"OK","totalCount":6,"vo":null,"list":[{
	 *         "id":"402880e44492d4ac014492d7f9c10004"
	 *         ,"name":"四川交通大学青年志愿者协会","image"
	 *         :null,"summary":"四川交通大学青少年行动起始于","cityType"
	 *         :{"id":"402880e44492d4ac014492e4a09b0009"
	 *         ,"name":"四川-成都","code":"028"
	 *         ,"parentCode":"0000"},"introduce":"四川交通大学青少年行动起始于1994年3月...."
	 *         ,"address"
	 *         :"四川省成都市武侯区科华路","character":"非营利性组织","manage":"四川交通大学团委"
	 *         ,"phone":"028-88888888"
	 *         ,"weibo":"http://weibo.com/schxyx","members"
	 *         :150},{"id":"402880e44492d4ac014492d533850001"
	 *         ,"name":"四川大学青年志愿者协会"
	 *         ,"image":null,"summary":"四川大学青少年行动起始于","cityType"
	 *         :{"id":"402880e44492d4ac014492e4a09b0009"
	 *         ,"name":"四川-成都","code":"028"
	 *         ,"parentCode":"0000"},"introduce":"四川大学青少年行动起始于1994年3月...."
	 *         ,"address"
	 *         :"四川省成都市武侯区科华路","character":"非营利性组织","manage":"四川大学团委","phone"
	 *         :"028-88888888"
	 *         ,"weibo":"http://weibo.com/schxyx","members":150}]}
	 */
	@POST
	@Path("/getOrganizations")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getOrganizationWithNameOrCity(
			@QueryParam("pageNo") int pageNo,
			@QueryParam("pageSize") int pageSize,
			@QueryParam("moduleCode") String moduleCode,
			@QueryParam("value") String value) {
		logger.info("getOrganizationWithNameOrCity called pageNo:" + pageNo
				+ " pageSize:" + pageSize + " moduleCode:" + moduleCode
				+ " value:" + value);
		ResultOrganizationVo result = new ResultOrganizationVo();
		String jpql = "from Organization o,Type t, ModuleOrganization mo,ModuleType mt where o.cityCode=t.code and (o.name=? or o.cityCode=?) and o.id=mo.organizationId and mo.moduleId=mt.id and mt.code=? order by o.name";
		String countSql = "select count(o.id) from Organization o, ModuleOrganization mo,ModuleType mt where (o.name=? or o.cityCode=?) and o.id=mo.organizationId and mo.moduleId=mt.id and mt.code=?";
		List<?> list = null;
		List<OrganizationVo> lt = new ArrayList<OrganizationVo>();
		result.setCode(Constant.SUCCESS_CODE);
		result.setMessage(Constant.OK);
		if ("".equals(value) || value == null) {
			jpql = "from Organization o,Type t, ModuleOrganization mo,ModuleType mt where o.cityCode=t.code and o.id=mo.organizationId and mo.moduleId=mt.id and mt.code=? order by o.name";
			countSql = "select count(o.id) from Organization o, ModuleOrganization mo,ModuleType mt where o.id=mo.organizationId and mo.moduleId=mt.id and mt.code=?";
			try {
				Object[] queryParams = { moduleCode };
				list = this.jpaDao.getResultData(pageNo, pageSize, jpql,
						queryParams);
				result.setTotalCount(this.jpaDao
						.getCount(countSql, queryParams));
			} catch (MyException e) {
				result.setCode(Constant.FAILE_CODE);
				result.setMessage(e.getMessage());
				e.printStackTrace();
			}
		} else {
			Object[] queryParams = { value, value, moduleCode };
			try {
				list = this.jpaDao.getResultData(pageNo, pageSize, jpql,
						queryParams);
				result.setTotalCount(this.jpaDao
						.getCount(countSql, queryParams));
			} catch (MyException e) {
				result.setCode(Constant.FAILE_CODE);
				result.setMessage(e.getMessage());
				e.printStackTrace();
			}

		}
		if (list != null && list.size() > Constant.ZERO) {
			for (Object o : list) {
				Object data[] = (Object[]) o;
				Organization organization = (Organization) data[Constant.ZERO];
				OrganizationVo vo = new OrganizationVo();
				vo.setAddress(organization.getAddress());
				vo.setCharacter(organization.getCharacter());
				vo.setCityType((Type) data[Constant.ONE]);
				vo.setId(organization.getId());
				if (organization.getImage() != null) {
					vo.setImage(this.serverPath + organization.getImage());
				}
				vo.setIntroduce(organization.getIntroduce());
				vo.setManage(organization.getManage());
				vo.setMembers(organization.getMembers());
				vo.setName(organization.getName());
				vo.setPhone(organization.getPhone());
				vo.setSummary(organization.getSummary());
				vo.setWeibo(organization.getWeibo());
				lt.add(vo);

			}
			result.setList(lt);
		}
		return Response.ok(result).build();
	}

	/**
	 * get base Organization with name or city code
	 * path:/organization/getBaseOrans Http:POST
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @param moduleCode
	 *            the code of module
	 * @param value
	 *            name of organization or city code of organization if ignore
	 *            the value will be null
	 * @return Response ResultBaseOrganizationVo with JSON String like:
	 *         {"code":268
	 *         ,"message":"OK","totalCount":6,"list":[{"id":"四川交通大学青年志愿者协会"
	 *         ,"name"
	 *         :null,"image":"null","summary":"四川交通大学青少年行动起始于","cityType":
	 *         {"id":"402880e44492d4ac014492e4a09b0009"
	 *         ,"name":"四川-成都","code":"028"
	 *         ,"parentCode":"0000"}},{"id":"四川大学青年志愿者协会"
	 *         ,"name":null,"image":"null"
	 *         ,"summary":"四川大学青少年行动起始于","cityType":{"id"
	 *         :"402880e44492d4ac014492e4a09b0009"
	 *         ,"name":"四川-成都","code":"028","parentCode":"0000"}}]}
	 */
	@POST
	@Path("/getBaseOrans")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getBaseOrganizationWithNameOrCity(
			@QueryParam("pageNo") int pageNo,
			@QueryParam("pageSize") int pageSize,
			@QueryParam("moduleCode") String moduleCode,
			@QueryParam("value") String value) {
		logger.info("getOrganizationWithNameOrCity called pageNo:" + pageNo
				+ " pageSize:" + pageSize + " value:" + value);
		ResultBaseOrganizationVo result = new ResultBaseOrganizationVo();
		String jpql = "select o.id,o.name,o.image,o.summary,o.cityCode from Organization o,ModuleOrganization mo,ModuleType mt where (o.name=? or o.cityCode=?) and o.id=mo.organizationId and mo.moduleId=mt.id and mt.code=? order by o.name";
		String countSql = "select count(o.id) from Organization o,ModuleOrganization mo,ModuleType mt where (o.name=? or o.cityCode=?) and o.id=mo.organizationId and mo.moduleId=mt.id and mt.code=?";
		List<?> list = null;
		List<BaseOrganizationVo> lt = new ArrayList<BaseOrganizationVo>();
		result.setCode(Constant.SUCCESS_CODE);
		result.setMessage(Constant.OK);
		if ("".equals(value) || value == null) {
			jpql = "select o.id,o.name,o.image,o.summary,o.cityCode from Organization o,ModuleOrganization mo,ModuleType mt where o.id=mo.organizationId and mo.moduleId=mt.id and mt.code=? order by o.name";
			countSql = "select count(o.id) from Organization o,ModuleOrganization mo,ModuleType mt where o.id=mo.organizationId and mo.moduleId=mt.id and mt.code=?";
			Object[] queryParams = { moduleCode };
			try {
				list = this.jpaDao.getResultData(pageNo, pageSize, jpql,
						queryParams);
				result.setTotalCount(this.jpaDao
						.getCount(countSql, queryParams));
			} catch (MyException e) {
				result.setCode(Constant.FAILE_CODE);
				result.setMessage(e.getMessage());
				e.printStackTrace();
			}
		} else {
			Object[] queryParams = { value, value, moduleCode };
			try {
				list = this.jpaDao.getResultData(pageNo, pageSize, jpql,
						queryParams);
				result.setTotalCount(this.jpaDao
						.getCount(countSql, queryParams));
			} catch (MyException e) {
				result.setCode(Constant.FAILE_CODE);
				result.setMessage(e.getMessage());
				e.printStackTrace();
			}

		}
		if (list != null && list.size() > Constant.ZERO) {
			for (Object o : list) {
				Object[] data = (Object[]) o;
				BaseOrganizationVo vo = new BaseOrganizationVo();
				vo.setCityType(this.getTypeByCode(String
						.valueOf(data[Constant.FOUR])));
				vo.setId(String.valueOf(data[Constant.ZERO]));
				vo.setName(String.valueOf(data[Constant.ONE]));
				if (data[Constant.TWO] != null) {
					vo.setImage(this.serverPath
							+ String.valueOf(data[Constant.TWO]));
				}
				vo.setSummary(String.valueOf(data[Constant.THREE]));
				lt.add(vo);

			}
			result.setList(lt);
		}
		return Response.ok(result).build();
	}

	/**
	 * get users belong to one organization path:/organization/getUsers
	 * Http:POST
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @param organizationId
	 *            primary key of organization if ignore the value will be null
	 * @return Response ResultBaseUserVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":10,"listVo":[{"id":
	 *         "402881e74481be89014481c1e74d0001"
	 *         ,"name":"李总","title":"心理咨询专家","image"
	 *         :"http://localhost:8082/ZP/upload/image/20144444444.jpg"
	 *         ,"introduce"
	 *         :"null"},{"id":"402881e64459ad0e014459af0a6e0005","name"
	 *         :"李林峰","title":"心理咨询专家","image":
	 *         "http://localhost:8082/ZP/upload/image/20144444444.jpg"
	 *         ,"introduce":"null"}]}
	 */
	@POST
	@Path("/getUsers")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getUserFromOrganization(@QueryParam("pageNo") int pageNo,
			@QueryParam("pageSize") int pageSize,
			@QueryParam("organizationId") String organizationId) {
		logger.info("getUserFromOrganization called pageNo:" + pageNo
				+ " pageSize:" + pageSize + " organizationId:" + organizationId);
		ResultBaseUserVo result = new ResultBaseUserVo();
		String jpql = "select u.id,u.name,u.title,u.image,u.introduce from User u,PersonOrganization o where u.id=o.userId and o.organizationId=? order by u.name";
		String countSql = "select count(u.id) from User u,PersonOrganization o where u.id=o.userId and o.organizationId=?";
		List<?> list = null;
		List<BaseUserVo> lt = new ArrayList<BaseUserVo>();
		result.setCode(Constant.SUCCESS_CODE);
		result.setMessage(Constant.OK);
		if ("".equals(organizationId) || organizationId == null) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(Constant.OBJECT_NOT_EXITS);
		} else {
			Object[] queryParams = { organizationId };
			try {
				list = this.jpaDao.getResultData(pageNo, pageSize, jpql,
						queryParams);
				result.setTotalCount(this.jpaDao
						.getCount(countSql, queryParams));
			} catch (MyException e) {
				result.setCode(Constant.FAILE_CODE);
				result.setMessage(e.getMessage());
				e.printStackTrace();
			}

		}
		if (list != null && list.size() > Constant.ZERO) {
			for (Object o : list) {
				Object[] data = (Object[]) o;
				BaseUserVo vo = new BaseUserVo();
				vo.setId(String.valueOf(data[Constant.ZERO]));
				vo.setName(String.valueOf(data[Constant.ONE]));
				vo.setTitle(String.valueOf(data[Constant.TWO]));
				if (data[Constant.THREE] != null) {
					vo.setImage(this.serverPath
							+ String.valueOf(data[Constant.THREE]));
				}
				vo.setIntroduce(String.valueOf(data[Constant.FOUR]));
				lt.add(vo);

			}
			result.setListVo(lt);
		}
		return Response.ok(result).build();
	}

	/**
	 * add person to organization path:/organization/addUser Http:POST
	 * 
	 * @param po
	 * 
	 * @return Response, ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":0}
	 */
	@POST
	@Path("/addUser")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	@Transactional
	public Response addUser(PersonOrganization po) {
		ResultCommonVo result = new ResultCommonVo();
		try {
			jpaDao.save(po);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * remove user from organization path:/organization/rmUser Http:DELETE
	 * 
	 * @param id
	 *            primary key of PersonOrganization
	 * 
	 * @return Response, ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":1}
	 */
	@DELETE
	@Path("/rmUser")
	@Produces(MediaType.APPLICATION_JSON)
	public Response removeUser(@QueryParam("id") String id) {
		logger.info("removeUser called id:" + id);
		ResultCommonVo result = new ResultCommonVo();
		try {
			jpaDao.deleteById(PersonOrganization.class, id);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * assign to one module path:/organization/assignModule Http:POST
	 * 
	 * @param mo
	 * 
	 * @return Response, ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":0}
	 */
	@POST
	@Path("/assignModule")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response assignModule(ModuleOrganization mo) {
		ResultCommonVo result = new ResultCommonVo();
		try {
			jpaDao.save(mo);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * remove organization from module path:/organization/rmFromModule
	 * Http:DELETE
	 * 
	 * @param id
	 *            primary key of ModuleOrganization
	 * 
	 * @return Response, ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":1}
	 */
	@DELETE
	@Path("/rmFromModule")
	@Produces(MediaType.APPLICATION_JSON)
	public Response removeOrg(@QueryParam("id") String id) {
		logger.info("removeOrg called id:" + id);
		ResultCommonVo result = new ResultCommonVo();
		try {
			jpaDao.deleteById(ModuleOrganization.class, id);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}
}
